package com.dms.modules.product.service;

import com.dms.modules.product.entity.Product;
import com.dms.modules.product.entity.ProductSku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品门面类，用于处理商品和SKU之间的交互
 */
@Component
public class ProductFacade {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSkuService skuService;

    /**
     * 检查商品是否属于指定商家
     */
    public boolean checkProductBelongsToMerchant(Long productId, Long merchantId) {
        Product product = productService.getById(productId);
        return product != null && product.getMerchantId().equals(merchantId);
    }

    /**
     * 获取商品的所有SKU
     */
    public List<ProductSku> getProductSkus(Long productId) {
        return skuService.getByProductId(productId);
    }

    /**
     * 删除商品及其SKU
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteProductWithSkus(Long productId) {
        skuService.deleteByProductId(productId);
        return productService.removeById(productId);
    }
} 